/*
    Dominate 3D - A cube-based game of life

    Copyright (C) 2007, Craig H. Miller (kidmosey@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TGA_H__
#define __TGA_H__

#include <image.h>
#include <stdio.h>

/// TGA Error Codes
enum {
    TGA_OK = 0,
    TGA_ERROR_FILEOPEN,
    TGA_ERROR_BADHEADER,
    TGA_ERROR_INVALIDIMAGE,
    TGA_ERROR_FILEREAD,
    TGA_ERROR_NOTENOUGHMEMORY
};

/// TGA Error Strings
const char * const TGA_ERRORS[] = {
    "No Error",
    "Unable to open file",
    "Invalid header",
    "Invalid image data",
    "File read error",
    "Not enough memory"
};

class CTGA: public CImage
{
public:
    /** A TGA header
     * this structure defines a targa header.
     * \bug could potentially be incompatible on other architectures/platforms
     *      since it isn't a packed structure.
     **/
    typedef struct
    {
        unsigned char		header[6];     ///< First 6 Useful Bytes From The Header
        unsigned int		bytesPerPixel; ///< Holds Number Of Bytes Per Pixel Used In The TGA File
        unsigned int		imageSize;	   ///< Used To Store The Image Size When Setting Aside Ram
        unsigned int		temp;		   ///< Temporary Variable
        unsigned int		type;
        unsigned int		Height;		   ///< Height of Image
        unsigned int		Width;		   ///< Width ofImage
        unsigned int		Bpp;		   ///< Bits Per Pixel
    } TGA;

    /// constructor
    CTGA(const char *fname): CImage(fname)
    {
        Load(fname);
    }

    /// destructor
    ~CTGA()
    {
    }

protected:
    /// load a tga file
    int Load(const char *fname);

private:
    /** Initialize the image information.
     *  Read the header information from a file and figure out
     *  which formats and encodings it is using
     */
    int InitStructures(FILE *);

    /// Read the bytes from the file
    int LoadUncompressedTGA(FILE *);

    /// Decompress an RLE compressed image.
    int LoadCompressedTGA(FILE *);
};

#endif // __TGA_H__
